import Vue from 'vue'
import VueRouter from 'vue-router'

import Layout from '@/views/layout/index'

Vue.use(VueRouter)

export const constantRoutes = [
  {
    path: '/',
    name: 'Login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/dashboard',
    redirect: "/dashboard/index",
    component: Layout,
    children: [{
      path: 'index',
      name: 'Home',
      component: () => import('@/views/home/index'),
      meta: { title: 'Home', icon: 'iconlayers' }
    }]
  }
]

export const asyncRoutes = [
  {
    path: '/permission',
    component: Layout,
    name: 'Permission',
    permit: ['admin', 'editor'],
    meta: { title: 'Permission', icon: 'iconmodular' },
    children: [
      {
        path: 'admin',
        name: 'admin',
        permit: ['admin'],
        component: () => import('@/views/permission/admin'),
        meta: { title: 'admin', icon: 'iconmove' }
      },
      {
        path: 'editor',
        name: 'editor',
        permit: ['editor'],
        component: () => import('@/views/permission/editor'),
        meta: { title: 'editor', icon: 'iconmove' }
      },
    ]
  },

  {
    path: '/example',
    component: Layout,
    name: 'Example',
    permit: ['admin', 'editor'],
    meta: { title: 'Example', icon: 'icontraining' },
    children: [
      {
        path: 'table',
        name: 'Table',
        permit: ['admin'],
        component: () => import('@/views/example/table'),
        meta: { title: 'Table', icon: 'iconmodular' }
      },
      {
        path: 'tree',
        name: 'Tree',
        permit: ['editor'],
        component: () => import('@/views/example/tree'),
        meta: { title: 'Tree', icon: 'iconmic' }
      },
    ]
  },

  {
    path: '/form',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Form',
        component: () => import('@/views/form/index'),
        meta: { title: 'Form', icon: 'iconmove' }
      }
    ]
  },

  {
    path: '/nested',
    component: Layout,
    name: 'Nested',
    meta: { title: 'Nested', icon: 'iconfilm' },
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/nested/menu1'),
        name: 'Menu1',
        meta: { title: 'menu1' }
      },
      {
        path: 'menu2',
        component: () => import('@/views/nested/menu2'),
        name: 'Menu2',
        meta: { title: 'menu2' },
        children: [
          {
            path: 'menu2-1',
            component: () => import('@/views/nested/menu2/menu2-1'),
            name: 'Menu2-1',
            meta: { title: 'menu2-1' }
          },
          {
            path: 'menu2-2',
            component: () => import('@/views/nested/menu2/menu2-2'),
            name: 'Menu2-2',
            meta: { title: 'menu2-2' },
            children: [
              {
                path: 'menu2-2-1',
                component: () => import('@/views/nested/menu2/menu2-2/menu2-2-1'),
                name: 'Menu2-2-1',
                meta: { title: 'menu2-2-1' },
              },
              {
                path: 'menu2-2-2',
                component: () => import('@/views/nested/menu2/menu2-2/menu2-2-2'),
                name: 'Menu2-2-2',
                meta: { title: 'menu2-2-2' },
              }
            ]
          },
          {
            path: 'menu2-3',
            component: () => import('@/views/nested/menu2/menu2-3'),
            name: 'Menu2-3',
            meta: { title: 'menu2-3' }
          },
        ]
      }
    ]
  }
]

export default new VueRouter({
  routes: constantRoutes
})

// 解决点击重复路由红色警告
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// export function resetRouter() {
//   const newRouter = createRouter()
//   router.matcher = newRouter.matcher // reset router
// }

